package com.xspace.android.xafcommon.view.dialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.xspace.android.xafcommon.R;
import com.xspace.android.xafcommon.util.LogUtil;

import java.io.File;


/**
 * Created by liuchen on 2016/10/31.
 */

public class ConfirmDialog extends Dialog {

    private TextView titleTv;
    private TextView msgTv;
    private ImageView imageView;
    private TextView leftTv;
    private TextView rightTv;
    private ButtonClickListener leftListener;
    private ButtonClickListener rightListener;
    private Context mContext;
    private View line;

    public ConfirmDialog(Context context, int theme, String strMessage) {
        super(context, theme);
        mContext = context;
        this.setContentView(R.layout.dialog_confirm);
        this.setCanceledOnTouchOutside(false);
//        this.setCancelable(false);
        findView();
        LogUtil.d("tvMsg>>" + strMessage);
        if (msgTv != null && !TextUtils.isEmpty(strMessage)) {
            msgTv.setText(strMessage);
        }
    }


    private void setLeftListener(ButtonClickListener listener) {
        this.leftListener = listener;
    }

    private void setRightListener(ButtonClickListener listener) {
        this.rightListener = listener;
    }

    private void findView() {
        titleTv = (TextView) findViewById(R.id.txt_title);
        LogUtil.d("titleTv isNull>>" + (titleTv == null));
        msgTv = (TextView) findViewById(R.id.txt_msg);
        LogUtil.d("msgTv isNull>>" + (msgTv == null));
        imageView = (ImageView) findViewById(R.id.img);
        LogUtil.d("imageView isNull>>" + (imageView == null));
        leftTv = (TextView) findViewById(R.id.txt_left);
        LogUtil.d("leftTv isNull>>" + (leftTv == null));
        rightTv = (TextView) findViewById(R.id.txt_right);
        LogUtil.d("rightTv isNull>>" + (rightTv == null));
        line = findViewById(R.id.line_vertical);
    }

    private void setListener() {
        leftTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (leftListener != null) {
                    leftListener.buttonClick(v);
                }
                dismiss();
            }
        });
        rightTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (rightListener != null) {
                    rightListener.buttonClick(v);
                }
                dismiss();
            }
        });
    }

    private void setMessageContent(String message) {
        msgTv.setText(message);
    }

    private void setMessageTitle(String message) {
        titleTv.setText(message);
    }

    private void setLeftText(String message) {
        if (!TextUtils.isEmpty(message)) {
            leftTv.setText(message);
        }
    }

    private void setRightText(String message) {
        if (!TextUtils.isEmpty(message)) {
            rightTv.setText(message);
        }
    }

    private void setRightTvGone() {
        rightTv.setVisibility(View.GONE);
    }

    private void setTitleVisible(int visible) {
        titleTv.setVisibility(visible);
    }

    private void setContentVisible(int visible) {
        msgTv.setVisibility(visible);
    }

    private void setImageVisible(int visible) {
        imageView.setVisibility(visible);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        if (!hasFocus) {
            dismiss();
        }
    }

    private void setImageView(String url, boolean isLocal) {
        if (isLocal) {
            Picasso.with(mContext)
                    .load(new File(url))
                    .placeholder(R.drawable.picturndefault1)
                    .error(R.drawable.picturndefault1)
                    .centerInside()
                    .resize(300, 300)
                    .into(imageView);
        } else {
            Picasso.with(mContext)
                    .load(url)
                    .placeholder(R.drawable.picturndefault1)
                    .error(R.drawable.picturndefault1)
                    .centerInside()
                    .resize(300, 300)
                    .into(imageView);
        }

    }

    /**
     * 显示圈圈
     */
    public void showDialog() {
        if (!isShowing()) {
            show();
        }
    }

    /**
     * @param @param id
     * @return void
     * @throws
     * @Description: 设置等待文本内容 int 类型
     */
    public void setMessage(int id) {
        msgTv.setText(id);
    }

    /**
     * @param @param msg
     * @return void
     * @throws
     * @Description: 设置等待文本内容 String
     */
    public void setMessage(String msg) {
        msgTv.setText(msg);
    }

    public void setMaxLine(int maxLine) {
        msgTv.setMaxLines(maxLine);
    }

    /**
     * 不显示圈圈
     */
    public void dismissProgress() {
        if (isShowing()) {
            dismiss();
        }
    }

    public static class Builder {
        ConfirmDialog mConfirmDialog;

        /**
         * Constructor using a context for this builder and the {@link AlertDialog} it creates.
         */
        public Builder(Context context, int theme) {
            mConfirmDialog = new ConfirmDialog(context, theme, "");
        }

        public Builder(Context context) {
            this(context, R.style.CustomDialog);
        }

        public Builder setContentMsg(String message) {
            mConfirmDialog.setMessageContent(message);
            return this;
        }

        public Builder setTitleMsg(String message) {
            mConfirmDialog.setMessageTitle(message);
            return this;
        }

        public Builder setLeftText(String leftText) {
            mConfirmDialog.setLeftText(leftText);
            return this;
        }

        public Builder setLeftListener(ButtonClickListener listener) {
            mConfirmDialog.setLeftListener(listener);
            return this;
        }

        public Builder setRightText(String rightText) {
            mConfirmDialog.setRightText(rightText);
            return this;
        }


        public Builder setRightListener(ButtonClickListener listener) {
            mConfirmDialog.setRightListener(listener);
            return this;
        }


        public Builder setListener() {
            mConfirmDialog.setListener();
            return this;
        }

        public void show() {
            mConfirmDialog.show();
        }

        public Builder setTitleVisible(int visible) {
            mConfirmDialog.setTitleVisible(visible);
            return this;
        }

        public Builder setContentVisible(int visible) {
            mConfirmDialog.setContentVisible(visible);
            return this;
        }

        public Builder setImageVisible(int visible) {
            mConfirmDialog.setImageVisible(visible);
            return this;
        }

        public Builder setImageSrc(String src, boolean isLocal) {
            mConfirmDialog.setImageView(src, isLocal);
            return this;
        }

        public Builder setMaxLine(int maxLine) {
            mConfirmDialog.setMaxLine(maxLine);
            return this;
        }

        public Builder setRightButtonGone() {
            mConfirmDialog.setRightTvGone();
            // 左边框背景改变  中间线的隐藏
            mConfirmDialog.leftTv.setBackgroundResource(R.drawable.bg_dialog_bottom_left_new);
            mConfirmDialog.line.setVisibility(View.GONE);
            return this;
        }
    }


}
